<?php
/**
 * GeoContexter
 * @link http://code.google.com/p/geocontexter/
 * @package GeoContexter
 */

/**
 * user add
 *
 * @package GeoContexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev: 828 $ / $LastChangedDate: 2011-02-27 10:30:28 +0100 (So, 27 Feb 2011) $ / $Author: armand.turpel $
 */

namespace Geocontexter\Controller;

use Zend\View\Model\ViewModel;
use Core\Controller\AbstractController;

class UserAddController extends AbstractController
{
   public function init()
    {
        $this->initView( 'geocontexter/user-add/index.phtml' );

        $this->view->partialData                = array('active_page' => 'add');
        $this->view->user_lastname              = '';
        $this->view->user_forename              = '';
        $this->view->user_login                 = '';
        $this->view->user_password              = '';
        $this->view->user_lang                  = 'en';
        $this->view->user_email                 = '';
        $this->view->user_url                   = '';
        $this->view->user_timezone              = '';
        $this->view->user_description           = '';
        $this->view->id_status                  = '';
        $this->view->id_group                   = '';
        $this->view->timezone_identifiers       = \DateTimeZone::listIdentifiers();
        $this->view->error                      = array();
    }

    public function indexAction()
    {
        $this->init_data();
        return $this->view;
    }

    private function init_data()
    {
        // get only languages that are flagged enabled=true
        //
        $LanguagesGet = $this->CoreModel('LanguagesGet');
        $params = array('enable' => 'true');

        $this->view->languages = $LanguagesGet->run( $params );
    }

    /**
     * add new group action
     */
    public function addAction()
    {
        // check on cancel action
        //
        $cancel = $this->request->getPost()->cancel;

        if ($cancel !== null) {
            return $this->redirect()->toUrl($this->getAdminBaseUrl() . '/user/index');
        }

        $params  = array();
        $error   = array();

        $this->view->id_status        =
            $params['id_status']      = $this->request->getPost()->id_status;

        $this->view->id_group         =
            $params['id_group']       = $this->request->getPost()->id_group;

        $this->view->user_lastname    =
            $params['user_lastname']  = $this->request->getPost()->user_lastname;

        $this->view->user_forename    =
            $params['user_forename']  = $this->request->getPost()->user_forename;

        $this->view->user_login       =
            $params['user_login']     = $this->request->getPost()->user_login;

        $user_password                = $this->request->getPost()->user_password;
        $user_password2               = $this->request->getPost()->user_password2;

        $this->view->user_lang        =
            $params['lang']           = $this->request->getPost()->user_lang;

        $this->view->user_email       =
            $params['user_email']     = $this->request->getPost()->user_email;

        $this->view->user_url         =
            $params['user_url']       = $this->request->getPost()->user_url;

        $this->view->user_timezone    =
            $params['user_timezone']  = $this->request->getPost()->user_timezone;

        $this->view->user_description =
            $params['description']    = $this->request->getPost()->user_description;

        if (empty($params['user_login'])) {
            $error[] = 'User login is empty';
        }

        if (empty($user_password) or empty($user_password2)) {
            $error[] = 'User password is empty';
        }

        if ($user_password != $user_password2) {
            $error[] = 'Both user password must be equal';
        }

        if (empty($params['user_lastname'])) {
            $error[] = 'User lastname is empty';
        }
        if (empty($params['user_forename'])) {
            $error[] = 'User forename is empty';
        }
        if (empty($params['user_email'])) {
            $error[] = 'User email is empty';
        }

        if (count($error) > 0) {
            $this->renderer->headTitle('Error: User data failure', 'PREPEND');
            $this->init_data();
            $this->view->error = $error;
            return $this->view;
        }

        $params['user_password'] = md5($user_password);

        $UserAdd = $this->CoreModel('UserAdd');

        $result = $UserAdd->run( $params );

        return $this->redirect()->toUrl($this->getAdminBaseUrl() . '/user/index');
    }
}

